<?php
require_once ( 'inc/mobile-detect.php' );
require_once ( 'inc/post-types/portfolio.php' );
require_once ( 'inc/post-types/team.php' );
require_once ( 'inc/post-types/project-title.php' );
//require_once ( 'inc/post-types/slider.php' );

// Define Theme Name for localization
define('THB_THEME_NAME', 'roboto');

function custom_wp_title( $title, $sep ) {
	global $paged, $page;

	if ( is_feed() ) {
		return $title;
	}

	// Add the site name.
	$title .= get_bloginfo( 'name', 'display' );

	// Add the site description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) ) {
		$title = "$title $sep $site_description";
	}

	// Add a page number if necessary.
	if ( $paged >= 2 || $page >= 2 ) {
		$title = "$title $sep " . sprintf( __( 'Page %s', THB_THEME_NAME ), max( $paged, $page ) );
	}

	return $title;
}
add_filter( 'wp_title', 'custom_wp_title', 10, 2 );

if ( ! function_exists( 'roboto_setup' ) ) :
function roboto_setup() {
	// Enable support for Post Thumbnails, and declare two sizes.
	add_theme_support( 'post-thumbnails' );
	set_post_thumbnail_size( 200, 300, true );
    add_image_size('team', 194, 245, true);
    add_image_size('involved-title', 206, 290, true);
    add_image_size('portfolio', 478, 535, true);

	// This theme uses wp_nav_menu() in two locations.
	register_nav_menus( array(
		'primary'   => __( 'Primary menu', THB_THEME_NAME ),
        'social'   => __( 'Social menu', THB_THEME_NAME )
	) );

}
endif; 
add_action( 'after_setup_theme', 'roboto_setup' );

// Translation
add_action('after_setup_theme', 'lang_setup');
function lang_setup(){
	load_theme_textdomain(THB_THEME_NAME, get_template_directory() . '/inc/languages');
}

// Visual Post Editor Button for Shortcodes
require_once ( 'inc/tinymce/tinymce-class.php' );	
require_once ( 'inc/tinymce/shortcode-processing.php' );
add_filter('widget_text', 'do_shortcode');

/**
 * Register three roboto widget areas.
 *
 * @since roboto 1.0
 */
function roboto_widgets_init() {
    if ( function_exists('register_sidebar') ){        
        register_sidebar(array('name' => 'Widget', 'id' => 'widget', 'description' => 'Primary Widget', 'before_widget' => '<div id="%1$s" class="widget cf %2$s">', 'after_widget' => '</div>', 'before_title' => '', 'after_title' => ''));
	}
}
add_action( 'widgets_init', 'roboto_widgets_init' );

//add new css & js
function list_style()  
{  
    wp_enqueue_script( 'jquery' );
    wp_register_script( 'bootstrap.js', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array(), null, true); 
    wp_enqueue_script( 'bootstrap.js' );
    
    if(is_page('contact-us')) {
        wp_register_script( 'maps_api', "https://maps.googleapis.com/maps/api/js?v=3.exp", array(), null, false); 
        wp_enqueue_script( 'maps_api' );
        wp_register_script( 'maps.js', get_template_directory_uri() . '/assets/js/maps.js', array(), null, false); 
        wp_enqueue_script( 'maps.js' );
    }
    
    wp_register_script( 'functions.js', get_template_directory_uri() . '/assets/js/functions.js', array(), null, true); 
    wp_enqueue_script( 'functions.js' );
    
    wp_register_style( 'boostrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css', array(), null); 
    wp_enqueue_style( 'boostrap' );
    wp_register_style( 'awesome', get_template_directory_uri() . '/assets/css/font-awesome.min.css', array(), null); 
    //wp_enqueue_style( 'awesome' );
    wp_register_style( 'styles.css', get_template_directory_uri() . '/assets/css/styles.css', array(), null); 
    wp_enqueue_style( 'styles.css' );
}  
add_action( 'wp_enqueue_scripts', 'list_style' );  

/**
 * Change url to dynamic
 **/
function dynamic_url($url) { //change static url
    $ary_img_url = explode("//",$url);
    if(count($ary_img_url) >= 2)
        $img_url_a = $ary_img_url[1];
    else
        $img_url_a = $ary_img_url[0];
    $img_url_a = explode("/",$img_url_a);
    $img_url = "/";
    $shift_array = array_shift($img_url_a);
    $img_url .= implode("/",$img_url_a);
    /*$img_url = "/";
    for($i = 1; $i < count($img_url_a); $i++) {
        $img_url .= "/".$img_url_a[$i]; 
    }*/
    return $img_url;
}

/* Remove Unwanted Tags */
function remove_invalid_tags($str, $tags) 
{
    foreach($tags as $tag)
    {
    	$str = preg_replace('#^<\/'.$tag.'>|<'.$tag.'>$#', '', $str);
    }

    return $str;
}

//hide admin bar
add_filter('show_admin_bar', '__return_false');

add_filter( 'option_posts_per_page', 'tdd_tax_filter_posts_per_page' );
function tdd_tax_filter_posts_per_page( $value ) {
    return (is_tax('portfolio_category')) ? 1 : $value;
}

/**
 * pagination
 **/
function pagination($pages = '', $range = 2, $tax_id) {   
     $showitems = ($range * 2)+1;  
     $category_link = get_term_link($tax_id, 'portfolio_category');
     global $paged;
     if(empty($paged)) $paged = 1;
 
     if($pages == '') {
        global $wp_query;
        $pages = $wp_query->max_num_pages;
        if(!$pages) {
            $pages = 1;
        }
     }   
 
     if(1 != $pages) {
        echo "<div class=\"pagination\" id='pagination'>";
        $next_link = get_pagenum_link($paged + 1);
        $next_link = explode("page", $next_link);
        $next_link = $category_link."page".$next_link[1];
        if ($paged < $pages) echo "<a href=\"".$next_link."\" class='next'>&rsaquo;</a>";  
        //if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
        echo "</div>\n";
     }
}